//
//  HomePageListHeadCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/29.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "HomePageListHeadCell.h"

@interface HomePageListHeadCell ()
@property (nonatomic,strong)UIImageView *headerImgView;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UIImageView *moreIcon;
@property (nonatomic,strong)UIButton *followBtn;
@property (nonatomic,strong)HomePageItemModel *model;
@property (nonatomic,strong)UIView *layerView;
@property (nonatomic,strong)UIView *whiteView;
@property (nonatomic,strong)CAGradientLayer *gradientLayer;
@property (nonatomic,strong)UIButton *moreBtn;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UILabel *timeLb;
@end


@implementation HomePageListHeadCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.headerImgView = [[UIImageView alloc]init];
    self.headerImgView.image = [UIImage imageNamed:@"defaut_header"];
    self.headerImgView.layer.cornerRadius = 20;
    self.headerImgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerImgView];
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.centerY.equalTo(self.contentView);
    }];
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.text = @"高考说";
    self.nameLb.textColor = C51;
    self.nameLb.font = [UIFont boldSystemFontOfSize:16];
    [self.contentView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerImgView).offset(4);
        make.left.equalTo(self.headerImgView.mas_right).offset(8);
    }];
    
    self.timeLb = [[UILabel alloc]init];
    self.timeLb.textColor = RGBA(107, 111, 122, 1);
    self.timeLb.font = font(10);
    [self.contentView addSubview:self.timeLb];
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(4);
        make.left.equalTo(self.nameLb);
    }];

    self.moreIcon = [[UIImageView alloc]init];
    self.moreIcon.image = [UIImage imageNamed:@"home_down_tag"];
    [self.contentView addSubview:self.moreIcon];
    
    [self.moreIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-16);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    
    self.moreBtn = [[UIButton alloc]init];
    [self.moreBtn addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.moreBtn];
    

    
    self.layerView = [[UIView alloc]init];
    self.layerView.layer.cornerRadius = 14;
    self.layerView.layer.masksToBounds = YES;
    self.layerView.backgroundColor = RGBA(245, 75, 100, 1);
    [self.contentView addSubview:self.layerView];
    
    self.whiteView = [[UIView alloc]init];
    self.whiteView.layer.cornerRadius = 13;
    self.whiteView.layer.masksToBounds = YES;
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.whiteView];
    
    self.followBtn = [[UIButton alloc]init];
    [self.followBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
    [self.followBtn setTitleColor:RGBA(245, 75, 100, 1) forState:UIControlStateNormal];
    self.followBtn.titleLabel.font = font(12);
    [self.followBtn addTarget:self action:@selector(followMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.followBtn];
    
    [self.layerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.followBtn).offset(-1);
        make.right.bottom.equalTo(self.followBtn).offset(1);
    }];
        
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.followBtn);
        make.right.bottom.equalTo(self.followBtn);
    }];
    
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.moreIcon);
        make.right.equalTo(self.moreIcon.mas_left).offset(-16);
        make.size.mas_equalTo(CGSizeMake(60,26));
    }];
    
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.contentView);
        make.left.equalTo(self.followBtn.mas_right).offset(5);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(248, 248, 248, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.contentView);
        make.height.mas_equalTo(8);
    }];
    self.line.hidden = YES;
    
    
    self.nameLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *name_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfoTap)];
    [self.nameLb addGestureRecognizer:name_tap];
    
    self.headerImgView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *head_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfoTap)];
    [self.headerImgView addGestureRecognizer:head_tap];
    
    self.moreIcon.userInteractionEnabled = YES;
    UITapGestureRecognizer *moreTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moreAction)];
    [self.moreIcon addGestureRecognizer:moreTap];

    

    
}

- (void)bindViewModel:(HomePageItemModel *)viewModel{
    /*
     0没有相互关系 1关注 2粉丝 3互相关注
     */
    
    self.model = viewModel;
    
    self.timeLb.text = viewModel.createDetailTimeStr;
    
    self.nameLb.text = viewModel.realName;
 
    if ([RPAppManager getCustomerId] && [viewModel.userId isEqualToNumber:[RPAppManager getCustomerId]]) {
        self.followBtn.hidden = YES;
        self.whiteView.hidden = YES;
        self.layerView.hidden = YES;
    }else{
        if (viewModel.followType.intValue == 3 || viewModel.followType.intValue == 1) {
            self.followBtn.hidden = YES;
            self.whiteView.hidden = YES;
            self.layerView.hidden = YES;
        }else if (viewModel.followType.intValue == 2){
            self.followBtn.hidden = NO;
            self.whiteView.hidden = NO;
            self.layerView.hidden = NO;
            [self.followBtn setTitle:@"+ 回关" forState:UIControlStateNormal];
        }else{
            self.followBtn.hidden = NO;
            self.whiteView.hidden = NO;
            self.layerView.hidden = NO;
            [self.followBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
        }
    }
    
    
    if ([viewModel.userId isEqualToNumber:[RPAppManager getCustomerId]]) {
        UserInfoModel *userInfo = [UserInfoModel userInfo];
        if (userInfo.imagePath.length == 0) {
            if (userInfo.sex.intValue == 0) {
                self.headerImgView.image = [UIImage imageNamed:@"defaut_header"];
            }else if (userInfo.sex.intValue == 1){
                self.headerImgView.image = [UIImage imageNamed:@"common_boy_tag"];
            }else{
                self.headerImgView.image = [UIImage imageNamed:@"common_girl_tag"];
            }
        }else{
            [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:userInfo.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
        }
    }else{
        if (viewModel.imagePath.length == 0) {
            if (viewModel.sex.intValue == 0) {
                self.headerImgView.image = [UIImage imageNamed:@"defaut_header"];
            }else if (viewModel.sex.intValue == 1){
                self.headerImgView.image = [UIImage imageNamed:@"common_boy_tag"];
            }else{
                self.headerImgView.image = [UIImage imageNamed:@"common_girl_tag"];
            }
        }else{
            [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:viewModel.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
        }

    }
    self.line.hidden = !viewModel.showLine;
  
}


- (void)followMethod{
    
    if ([RPAppManager isShowNetworkView]) {
        [WHToast showErrorWithMessage:@"网络好像有点问题，请检查后重试" duration:2 finishHandler:nil];
        return;
    }
    
    if (self.model.followType.intValue == 2) {
        [MobClick event:@"homePage_follow_back"];
    }else{
        [MobClick event:@"homePage_follow"];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(followUserInfo:)]) {
        [self.delegate followUserInfo:self.model.userId.stringValue];
    }
}

- (void)showUserInfoTap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfo:)]) {
        [self.delegate showUserInfo:self.model.userId.stringValue];
    }
}

- (void)moreAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showMoreViewWithCurrentModel:)]) {
        [self.delegate showMoreViewWithCurrentModel:self.model];
    }
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    self.gradientLayer =  [CAGradientLayer layer];
    self.gradientLayer.frame = self.layerView.bounds;
    self.gradientLayer.endPoint = CGPointMake(0, 0);
    self.gradientLayer.startPoint  = CGPointMake(1.0, 0);
    self.gradientLayer.colors = @[(id)RGBA(247, 131, 97, 1).CGColor,(id)RGBA(245, 75, 100, 1).CGColor];
    [self.layerView.layer addSublayer:self.gradientLayer];

    if (self.model.showRadius) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(14, 14)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}




@end
